package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityParam;
import com.sailfish.springbootdemo.pojo.db2.PcbSonFactoryInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbSonFactoryInfoDao {

    @Results(id = "pcbSonFactoryInfoResultMap", value = {
            @Result(column = "pcb_son_factory_id", property = "pcbSonFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.INTEGER),
            @Result(column = "company_name", property = "companyName", jdbcType = JdbcType.INTEGER),
            @Result(column = "company_name_en", property = "companyNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_code", property = "companyCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_website", property = "companyWebsite", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_sort", property = "companySort", jdbcType = JdbcType.INTEGER),
            @Result(column = "son_factory_name", property = "sonFactoryName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "son_factory_name_en", property = "sonFactoryNameEn", jdbcType = JdbcType.INTEGER),
            @Result(column = "son_factory_introduction", property = "sonFactoryIntroduction", jdbcType = JdbcType.VARCHAR),
            @Result(column = "son_factory_introduction_en", property = "sonFactoryIntroductionEn", jdbcType = JdbcType.INTEGER),
            @Result(column = "son_factory_contacts", property = "sonFactoryContacts", jdbcType = JdbcType.INTEGER),
            @Result(column = "son_factory_phone", property = "sonFactoryPhone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "son_factory_email", property = "sonFactoryEmail", jdbcType = JdbcType.VARCHAR),
            @Result(column = "son_factory_category", property = "sonFactoryCategory", jdbcType = JdbcType.VARCHAR),
            @Result(column = "son_factory_category_en", property = "sonFactoryCategoryEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark_pic", property = "remarkPic", jdbcType = JdbcType.VARCHAR),
    })

    /*
     *查询单个
     */
    @Select("<script> " +
            "select pcb_son_factory_id, parent_id, company_name, company_name_en, company_code, company_website, company_sort, son_factory_name, son_factory_name_en, son_factory_introduction, son_factory_introduction_en, son_factory_contacts, son_factory_phone, son_factory_email, son_factory_category, son_factory_category_en, create_time, update_time, status, sort " +
            " from " + DB2TableName.PCB_SON_FACTORY +
            " where  pcb_son_factory_id = #{id}" +
            "</script>")
    PcbSonFactoryInfo getById(@Param("id") Integer id);

    /*
     *查询指定行数据
     */
    @Select("<script> " +
            "select a.pcb_son_factory_id, a.parent_id, a.company_name, a.company_name_en, a.company_code, a.company_website, a.company_sort, a.son_factory_name, a.son_factory_name_en, a.son_factory_introduction, a.son_factory_introduction_en, a.son_factory_contacts, a.son_factory_phone, a.son_factory_email, a.son_factory_category, a.son_factory_category_en, a.create_time, a.update_time, a.status, a.sort,b.remark_pic " +
            "from " + DB2TableName.PCB_SON_FACTORY + " a LEFT JOIN " + DB2TableName.PCB_PARENT_FACTORY +" b ON a.parent_id = b.pcb_parent_factory_id" +
            " where 1 = 1 " +
            "<if test='pcbSonFactoryId != null'>" +
            " and (a.pcb_son_factory_id = #{pcbSonFactoryId})" +
            "</if>" +
            "<if test='parentId != null'>" +
            " and (a.parent_id = #{parentId})" +
            "</if>" +
            "<if test='companyName != null and companyName != \"\" ' >" +
            " and (a.company_name like concat('%', #{companyName}, '%') )" +
            "</if>" +
            "<if test='companyNameEn != null and companyNameEn != \"\"'>" +
            " and (a.company_name_en like concat('%', #{companyNameEn}, '%'))" +
            "</if>" +
            "<if test='companyCode != null and companyCode != \"\"'>" +
            " and (a.company_code like concat('%', #{companyCode}, '%') )" +
            "</if>" +
            "<if test='companyWebsite != null and companyWebsite != \"\"'>" +
            " and (a.company_website like concat('%', #{companyWebsite}, '%') )" +
            "</if>" +
            "<if test='sonFactoryName != null and sonFactoryName != \"\"'>" +
            " and (a.son_factory_name like concat('%', #{sonFactoryName}, '%') )" +
            "</if>" +
            "<if test='sonFactoryNameEn != null and sonFactoryNameEn != \"\"'>" +
            " and (a.son_factory_name_en like concat('%', #{sonFactoryNameEn}, '%') )" +
            "</if>" +
            "<if test='sonFactoryIntroduction != null and sonFactoryIntroduction != \"\"'>" +
            " and (a.son_factory_introduction like concat('%', #{sonFactoryIntroduction}, '%') )" +
            "</if>" +
            "<if test='sonFactoryIntroductionEn != null and sonFactoryIntroductionEn != \"\"'>" +
            " and (a.son_factory_introduction_en like concat('%', #{sonFactoryIntroductionEn}, '%') )" +
            "</if>" +
            "<if test='sonFactoryContacts != null and sonFactoryContacts != \"\"'>" +
            " and (a.son_factory_contacts like concat('%', #{sonFactoryContacts}, '%') )" +
            "</if>" +
            "<if test='sonFactoryCategory != null and sonFactoryCategory != \"\"'>" +
            " and (a.son_factory_category like concat('%', #{sonFactoryCategory}, '%') )" +
            "</if>" +
            "<if test='sonFactoryCategoryEn != null and sonFactoryCategoryEn != \"\"'>" +
            " and (a.son_factory_category_en like concat('%', #{sonFactoryCategoryEn}, '%') )" +
            "</if>" +
            "<if test='status != null'>" +
            " and (a.status = #{status})" +
            "</if>" +
            " order by a.company_sort,a.sort" +
            "</script>")

    @ResultMap(value = "pcbSonFactoryInfoResultMap")
    List<PcbSonFactoryInfo> getAllByLimit(PcbSonFactoryInfo pcbSonFactoryInfo);

    /*
     *查询所有板厂id
     */
    @Select("<script> " +
            "select pcb_son_factory_id " +
            " from " + DB2TableName.PCB_SON_FACTORY +
            "</script>")
    List<Integer> getAllId();

    /*
     *统计总行数
     */
    @Select("<script> " +
            "select count(1) " +
            "from " + DB2TableName.PCB_SON_FACTORY +
            " where 1=1 " +
//            "<if test='pcbSonFactoryId != null'>" +
//            " and (pcb_son_factory_id = #{pcbSonFactoryId})" +
//            "</if>" +
            "<if test='parentId != null'>" +
            " and (parent_id = #{parentId})" +
            "</if>" +
            "<if test='companyName != null and companyName != \"\" ' >" +
            " and (company_name = #{companyName})" +
            "</if>" +
            "<if test='companyCode != null and companyCode != \"\" ' >" +
            " and (company_code = #{companyCode})" +
            "</if>" +
            "<if test='companyNameEn != null and companyNameEn != \"\"'>" +
            " and (company_name_en = #{companyNameEn}))" +
            "</if>" +
            "<if test='companyWebsite != null and companyWebsite != \"\"'>" +
            " and (company_website = #{companyWebsite})" +
            "</if>" +
            "<if test='sonFactoryName != null and sonFactoryName != \"\"'>" +
            " and (son_factory_name = #{sonFactoryName})" +
            "</if>" +
            "<if test='sonFactoryNameEn != null and sonFactoryNameEn != \"\"'>" +
            " and (son_factory_name_en = #{sonFactoryNameEn})" +
            "</if>" +
            "<if test='sonFactoryIntroduction != null and sonFactoryIntroduction != \"\"'>" +
            " and (son_factory_introduction = #{sonFactoryIntroduction})" +
            "</if>" +
            "<if test='sonFactoryIntroductionEn != null and sonFactoryIntroductionEn != \"\"'>" +
            " and (son_factory_introduction_en = #{sonFactoryIntroductionEn})" +
            "</if>" +
            "<if test='sonFactoryContacts != null and sonFactoryContacts != \"\"'>" +
            " and (son_factory_contacts = #{sonFactoryContacts})" +
            "</if>" +
            "<if test='sonFactoryCategory != null and sonFactoryCategory != \"\"'>" +
            " and (son_factory_category = #{sonFactoryCategory})" +
            "</if>" +
            "<if test='sonFactoryCategoryEn != null and sonFactoryCategoryEn != \"\"'>" +
            " and (son_factory_category_en = #{sonFactoryCategoryEn})" +
            "</if>" +
            "<if test='status != null'>" +
            " and (status = #{status})" +
            "</if>" +
            " order by sort" +
            "</script>")
    Integer getAllCount(PcbSonFactoryInfo pcbSonFactoryInfo);

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_SON_FACTORY + " (parent_id, company_name, company_name_en, company_website, company_code, company_sort, son_factory_name, son_factory_name_en, son_factory_introduction, son_factory_introduction_en, son_factory_contacts, son_factory_phone, son_factory_email, son_factory_category, son_factory_category_en, create_time, update_time, status, sort) values " +
            "(#{parentId}, #{companyName}, #{companyNameEn}, #{companyWebsite}, #{companyCode}, #{companySort},#{sonFactoryName}, #{sonFactoryNameEn}, #{sonFactoryIntroduction}, #{sonFactoryIntroductionEn}, #{sonFactoryContacts}, #{sonFactoryPhone}, #{sonFactoryEmail}, #{sonFactoryCategory}, #{sonFactoryCategoryEn}, #{createTime}, #{updateTime}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "pcbSonFactoryId")
    Integer insert(PcbSonFactoryInfo param);

    /*
     *批量新增
     */
    @Insert("insert into " + DB2TableName.PCB_SON_FACTORY + " (parent_id, company_name, company_name_en, company_website, company_code, company_sort, son_factory_name, son_factory_name_en, son_factory_introduction, son_factory_introduction_en, son_factory_contacts, son_factory_phone, son_factory_email, son_factory_category, son_factory_category_en, create_time, update_time, status, sort) values " +
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(#{entity.parentId}, #{entity.companyName}, #{entity.companyNameEn}, #{entity.companyWebsite}, #{entity.companyCode}, #{companySort}, #{entity.sonFactoryName}, #{entity.sonFactoryNameEn}, #{entity.sonFactoryIntroduction}, #{entity.sonFactoryIntroductionEn}, #{entity.sonFactoryContacts}, #{entity.sonFactoryPhone}, #{entity.sonFactoryEmail}, #{entity.sonFactoryCategory}, #{entity.sonFactoryCategoryEn}, #{entity.createTime}, #{entity.updateTime}, #{entity.status}, #{entity.sort})")
    Integer insertBatch(@Param("entities") List<PcbSonFactoryInfo> entities);


    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_SON_FACTORY + " where pcb_son_factory_id = #{id} ")
    Integer delete(@Param("id") Integer id);

    @Delete("delete from " + DB2TableName.PCB_SON_FACTORY + " where  pcb_son_factory_id in " +
            "<foreach collection=\"ids\" item = \"id\" open=\"(\" separator=\",\" close=\")\">  " +
            "#{id} " +
            "</foreach>"
    )
    Integer deleteBatch(@Param("ids") List<Integer> ids);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_SON_FACTORY +
            "<set>" +
            "<if test='parentId != null'>" +
            "parent_id = #{parentId}," +
            "</if>" +
            "<if test='companyName != null and companyName != \"\"'>" +
            "company_name = #{companyName}," +
            "</if>" +
            "<if test='companyNameEn != null and companyNameEn != \"\"'>" +
            "company_name_en = #{companyNameEn}," +
            "</if>" +
            "<if test='companyWebsite != null '>" +//and companyWebsite != ""
            "company_website = #{companyWebsite}," +
            "</if>" +
            "<if test='companyCode != null '>" +//and companyWebsite != ""
            "company_code = #{companyCode}," +
            "</if>" +
            "<if test='companySort != null '>" +//and companyWebsite != ""
            "company_sort = #{companySort}," +
            "</if>" +
            "<if test='sonFactoryName != null '>" +//and sonFactoryName != ""
            "son_factory_name = #{sonFactoryName}," +
            "</if>" +
            "<if test='sonFactoryNameEn != null and sonFactoryNameEn != \"\"'>" +
            "son_factory_name_en = #{sonFactoryNameEn}," +
            "</if>" +
            "<if test='sonFactoryIntroduction != null '>" +//and sonFactoryIntroduction != ""
            "son_factory_introduction = #{sonFactoryIntroduction}," +
            "</if>" +
            "<if test='sonFactoryIntroductionEn != null '>" +//and sonFactoryIntroductionEn != ""
            "son_factory_introduction_en = #{sonFactoryIntroductionEn}," +
            "</if>" +
            "<if test='sonFactoryContacts != null '>" +//and sonFactoryContacts != ""
            "son_factory_contacts = #{sonFactoryContacts}," +
            "</if>" +
            "<if test='sonFactoryPhone != null '>" +//and sonFactoryPhone != ""
            "son_factory_phone = #{sonFactoryPhone}," +
            "</if>" +
            "<if test='sonFactoryEmail != null '>" +//and sonFactoryEmail != ""
            "son_factory_email = #{sonFactoryEmail}," +
            "</if>" +
            "<if test='sonFactoryCategory != null '>" +//and sonFactoryCategory != ""
            "son_factory_category = #{sonFactoryCategory}," +
            "</if>" +
            "<if test='sonFactoryCategoryEn != null '>" +//and sonFactoryCategoryEn != ""
            "son_factory_category_en = #{sonFactoryCategoryEn}," +
            "</if>" +
            "<if test='createTime != null '>" +
            "create_time = #{createTime}," +
            "</if>" +
            "<if test='updateTime != null '>" +
            "update_time = #{updateTime}," +
            "</if>" +
            "<if test='status != null'>" +
            "status = #{status}," +
            "</if>" +
            "<if test='sort != null'>" +
            "sort = #{sort} " +
            "</if>" +
            "</set>" +
            "where pcb_son_factory_id = #{pcbSonFactoryId}" +
            "</script>")
    Integer update(PcbSonFactoryInfo param);
}
